package engineer.clientapp.actions;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import engineer.clientapp.beans.User;
import engineer.clientapp.tools.Connector;
import engineer.clientapp.tools.Tool;

public class LoginAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session;
		String target = null;
		User loginForm = (User) form;
		String login = loginForm.getEmail();
		String password = DigestUtils.md5Hex(loginForm.getPassword());
		ActionErrors actionErrors = new ActionErrors();

		System.out.println(login + password);

		if (!(login == null || login.equals(""))) {
			if (!(password == null || password.equals(""))) {
				if (Tool.checkEmailFormat(login)) {
					try {

						Connection connection = new Connector().getConnection();
						Statement statement = connection.createStatement();
						statement.setMaxRows(1);

						ResultSet result = statement
								.executeQuery("SELECT id,email,password,name,surname,address,pesel, foto, type FROM automats.users WHERE email=\""
										+ login
										+ "\" && password=\""
										+ password + "\"");

						if (result.next()) {

							session = request.getSession(true);
							session.setAttribute(
									"user",
									new User(result.getInt(1), result
											.getString(2), result.getString(3),
											result.getString(4), result
													.getString(5), result
													.getString(6), result
													.getLong(7), result.getString(8), result.getString(9)));
							target = "success";
							result.close();
							connection.close();

						} else {

							target = "failure";

							actionErrors.add("info", new ActionMessage(
									"error.password"));
							result.close();
							connection.close();
						}
					} catch (SQLException e) {
						target = "failure";
						actionErrors.add("info", new ActionMessage(
								"error.catch"));
						e.printStackTrace();
					}
				} else {
					target = "failure";
					actionErrors.add("info", new ActionMessage(
							"error.emailFormat"));
				}
			} else {
				target = "failure";
				actionErrors.add("info", new ActionMessage(
						"error.emptyPassword"));
			}
		} else {
			// asdasd
			target = "failure";
			actionErrors.add("info", new ActionMessage("error.emptyemail"));
		}

		if (actionErrors.size() != 0) {
			extracted(request, actionErrors);
			return new ActionForward(mapping.getInput());
		}
		return mapping.findForward(target);

	}

	private void extracted(HttpServletRequest request, ActionErrors actionErrors) {
		saveErrors(request, actionErrors);
	}

}
